/*
 * Animation.java
 * 
 * Created on Nov 14, 2007, 8:12:20 PM
 * 
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.apps.rtemaster.gui.map;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import org.fpdev.apps.rtemaster.analysis.Visualizer;
import org.fpdev.apps.rtemaster.gui.RMGUI;

/**
 *
 * @author demory
 */
public class Animation implements ActionListener {

  private Visualizer visualizer_;
  private RMGUI gui_;

  private int now_,  step_,  start_,  end_,  fps_;

  private Timer timer_;
  
  public Animation(int start, int end, int fps, int speed, RMGUI gui, Visualizer visualizer) {
    start_ = start;
    end_ = end;
    fps_ = fps;
    gui_ = gui;
    visualizer_ = visualizer;

    int timerStep = 1000 / fps;
    double stepd = ((double) timerStep / 1000) * speed;
    step_ = (int) stepd;
    timer_ = new Timer(timerStep, this);
  }

  @Override
  public void actionPerformed(ActionEvent e) {
    //System.out.println(">>> tick "+now_);

    visualizer_.refreshSnapshot(now_);

    gui_.getMapPanel().refresh(false, false, true);

    now_ += step_;
    gui_.getAnalysisOptionsPanel().getAnimatorPanel().setProgress(now_);
    if (now_ >= end_) {
      if (gui_.getAnalysisOptionsPanel().getAnimatorPanel().getDoRepeat()) {
        timer_.stop();
        start();
      } else {
        timer_.stop();
        gui_.getMapPanel().setIsAnimating(false);
        gui_.getMapPanel().refresh(false, false, true);
      }
    }
  }

  @SuppressWarnings("unchecked")
  public void start() {
    now_ = start_;
    timer_.start();
    System.out.println("started");
  }

  public void pause() {
    timer_.stop();
  }

  public void resume() {
    timer_.restart();
  }

  public int getStartTime() {
    return start_;
  }

  public int getEndTime() {
    return end_;
  }
}
